package com.qf.controller;

import com.qf.pojo.DtsOrder;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/19 10:58
 * description:
 */

@RestController
@RequestMapping("/admin/dashboard")
public class AdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsProductService productService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;


    @RequestMapping("")
    public Object dashboard(){
        Map<String,Object> data=new HashMap<>();

        //商品数量
        Integer goodsTotal=goodsService.getTotal();
        data.put("goodsTotal",goodsTotal);

        //；用户数量
        Integer userTotal=userService.getTotal();

        data.put("userTotal",userTotal);

        //货品数量
        Integer productTotal=productService.getTotal();

        data.put("productTotal",productTotal);

        //订单数量
        Integer orderTotal=orderService.getTotal();

        data.put("orderTotal",orderTotal);

        return ResponseUtil.ok(data);
    }


    @RequestMapping("/chart")
    public Object chart(){


        Map<String ,Object> data=new HashMap<>();
        //categorySell， 商品分类和对应的销售额
        //orderAmts，日期和对应的订单总额，订单笔数
        //userOrderCnt，日期对应的用户数和订单数

        List<CategorySellAmts> categorySellData =categoryService.findCategorySellData();

        String[] categoryNames=new String[categorySellData.size()];
        for (int i=0;i<categorySellData.size();i++){
            CategorySellAmts categorySellAmts = categorySellData.get(i);
            categoryNames[i]=categorySellAmts.getName();
        }

        CategorySellVo categorySellVo=new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(categorySellData);

        //查询最近一个月 每天的订单总额和订单笔数

        List<DayStatis> orderStatis=orderService.getOrderAmts();

        String[] dayStr=new String[orderStatis.size()];
        int[] cnts=new int[orderStatis.size()];
        BigDecimal[] amts=new BigDecimal[orderStatis.size()];
        for(int i=0;i<orderStatis.size();i++){
            DayStatis dayStatis = orderStatis.get(i);
            dayStr[i]=dayStatis.getDayStr();
            cnts[i] = dayStatis.getCnts();
            amts[i]=dayStatis.getAmts();
        }


        OrderAmtsVo orderAmtsVo=new OrderAmtsVo();
        //日期列表
        orderAmtsVo.setDayData(dayStr);
        //订单总额
        orderAmtsVo.setOrderCntData(cnts);
        //订单数量
        orderAmtsVo.setOrderAmtData(amts);

        //统计一个月内，用户和订单每天的数量
        List<DayStatis> userStatis=userService.findUserStatis();
        String[] uodayStr=new String[userStatis.size()];
        int[] uocnts=new int[userStatis.size()];
        UserOrderCntVo userOrderCntVo=new UserOrderCntVo();
        for(int i=0;i<userStatis.size();i++){
            DayStatis dayStatis = userStatis.get(i);
            uodayStr[i]=dayStatis.getDayStr();
            uocnts[i] = dayStatis.getCnts();
        }
        userOrderCntVo.setDayData(uodayStr);
        userOrderCntVo.setUserCnt(uocnts);
        userOrderCntVo.setOrderCnt(cnts);


        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmtsVo);
        data.put("userOrderCnt",userOrderCntVo);


        return ResponseUtil.ok(data);
    }


}
